package tr.edu.metu.is.mega.client.course.announcement;

import java.util.Date;

import tr.edu.metu.is.mega.client.core.AuthUtils;
import tr.edu.metu.is.mega.client.core.EditDialog;
import tr.edu.metu.is.mega.client.core.QueryCallback;
import tr.edu.metu.is.mega.client.widgets.MegaTextArea;
import tr.edu.metu.is.mega.client.widgets.MegaTextField;
import tr.edu.metu.is.mega.common.dto.AnnouncementDTO;
import tr.edu.metu.is.mega.common.dto.BaseDTO;
import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.common.dto.UserDTO;

import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FormData;

/**
 * Edit dialog for {@link AnnouncementDTO}
 */
public class AnnouncementEditDialog extends EditDialog<AnnouncementDTO> {

    private MegaTextArea descriptionField;

    private MegaTextField userField;

    private FormBinding binding;

    private BeanModelFactory beanModelFactory;

    private CourseDTO course;

    /**
     * Default constructor.
     */
    public AnnouncementEditDialog(CourseDTO course) {
        super(AnnouncementDTO.class.getName(), course);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void beforeInitForm(BaseDTO baseDTO) {
        this.course = (CourseDTO) baseDTO;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initForm(FormPanel parent, Object... params) {
        setSize(500, 180);
        setHeading("Edit Announcement");
        parent.setLabelWidth(120);

        beanModelFactory = BeanModelLookup.get().getFactory(AnnouncementDTO.class);

        descriptionField = new MegaTextArea(AnnouncementDTO.FIELD_TEXT);
        descriptionField.setFieldLabel("Announcement");
        parent.add(descriptionField, new FormData("96%"));

        userField = new MegaTextField(AnnouncementDTO.FIELD_CREATEDBY);
        userField.setFieldLabel("Created By");
        userField.setReadOnly(true);
        parent.add(userField, new FormData("96%"));

        binding = new FormBinding(parent, true);

        binding.bind(beanModelFactory.createModel(new AnnouncementDTO()));

        AuthUtils.getInstance().getCurrentUser(new QueryCallback<UserDTO>() {

            @Override
            public void onQueryResult(UserDTO result) {
                userField.setValue(result != null ? result.getFullName() : "System Message");
            }
        });

    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected AnnouncementDTO guiToDTO() {
        BeanModel model = (BeanModel) binding.getModel();
        AnnouncementDTO dto = model.getBean();
        dto.setCourse(course);
        dto.setDateCreated(new Date());
        return dto;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void dtoToGUI(AnnouncementDTO dto) {
        binding.bind(beanModelFactory.createModel(dto));
    }

}
